package com.duan.laugh.mp.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.duan.laugh.common.core.util.Query;
import com.duan.laugh.common.core.util.R;
import com.duan.laugh.mp.api.pojo.entity.MpTemplateMsg;
import com.duan.laugh.mp.config.WxMpConfiguration;
import com.duan.laugh.mp.service.IMpTemplateMsgService;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/wxtemplatemsg")
public class MpTemplateMsgController {

    @Autowired
    private IMpTemplateMsgService mpTemplateMsgService;

    /**
     * 新增维护微信模板消息
     *
     * @param mpTemplateMsg 实体
     * @return true/false
     */
    @PostMapping
    @ApiOperation("新增维护微信模板消息")
    public R save(@RequestBody MpTemplateMsg mpTemplateMsg) {
        return R.ok(mpTemplateMsgService.save(mpTemplateMsg));
    }

    /**
     * 根据id查看维护微信模板消息
     *
     * @param id
     * @return MpTemplateMsg
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查看维护微信模板消息")
    public R<MpTemplateMsg> getById(@PathVariable String id) {
        return R.ok(mpTemplateMsgService.getById(id));
    }

    /**
     * 分页查询维护微信模板消息
     *
     * @param params 分页对象
     * @return 分页对象
     */
    @PostMapping("/page")
    @ApiOperation("分页查询维护微信模板消息")
    public IPage<MpTemplateMsg> page(@RequestBody Map<String, Object> params) {
        return mpTemplateMsgService.page(new Query<>(params));
    }

    /**
     * 根据id删除维护微信模板消息
     *
     * @param id
     * @return true/false
     */
    @DeleteMapping("/{id}")
    @ApiOperation("根据id删除维护微信模板消息")
    public R removeById(@PathVariable String id) {
        return R.ok(mpTemplateMsgService.removeById(id));
    }

    /**
     * 根据id修改维护微信模板消息
     *
     * @param mpTemplateMsg 实体
     * @return true/false
     */
    @PutMapping
    @ApiOperation("根据id修改维护微信模板消息")
    public R updateById(@RequestBody MpTemplateMsg mpTemplateMsg) {
        return R.ok(mpTemplateMsgService.updateById(mpTemplateMsg));
    }


//    @PostMapping
    @SneakyThrows
    public R sendTemplateMsg(@RequestBody Map map) {
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser("oFgpJwNZ1X_iOrB-0mx3vCHHSgQE")
                .templateId("-YxkG1x5jKu06g8ePzUagmgkHlnAkLvFtVIRLLnuDI4")
                .url("http://www.baidu.com")
                .build();

        templateMessage.addData(new WxMpTemplateData("first", "亲，你的申请已处理！！！"));
        templateMessage.addData(new WxMpTemplateData("keyword1", "请假"));
        templateMessage.addData(new WxMpTemplateData("keyword2", "创新中心部门经理-付权勇"));
        templateMessage.addData(new WxMpTemplateData("keyword3", "骚年，世界这么乱还在在公司呆着吧！", "#FF0000"));
//        templateMessage.addData(new WxMpTemplateData("keyword4", "不同意","#FF0000"));
        templateMessage.addData(new WxMpTemplateData("remark", "点击，查看详情~"));
        WxMpService wxMpService = WxMpConfiguration.getMpServices().get("wxf46b5ef1a1cd6170");
//        wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);

//        for (int i = 0; i < 20; i++) {
        wxMpService.getKefuService().sendKefuMessage(WxMpKefuMessage
                .TEXT()
                .toUser("oFgpJwNZ1X_iOrB-0mx3vCHHSgQE")
                .content(map.get("content").toString())
                .build());
//        }

        return R.ok();
    }
}
